package com.tyk.bookstore.back.comment.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.comment.model.query.CommentPageQuery;
import com.tyk.bookstore.back.comment.model.vo.CommentPageVo;
import com.tyk.bookstore.back.comment.model.vo.CommentVo;
import com.tyk.bookstore.back.comment.service.CommentService;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author tyk
 * @description 评论管理
 */
@Tag(name = "评论管理/评论管理", description = "评论管理")
@RequestMapping({"api/message/comment"})
@RestController
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    @Operation(summary = "查询评论详情", description = "查询评论详情")
    @SaCheckPermission("message:comment:get")
    @GetMapping({"{id}"})
    public R<CommentVo> getById(
            @Schema(description = "评论ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(commentService.getById(id));
    }

    @Operation(summary = "分页查询评论", description = "分页查询评论")
    @SaCheckPermission("message:comment:page")
    @GetMapping({"page"})
    public R<CommentPageVo> getPage(
            @Validated @ParameterObject CommentPageQuery query, BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(CommentPageVo.class)) : R.ok(commentService.getPage(query));
    }

    @Operation(summary = "删除评论", description = "删除商品评论")
    @SaCheckPermission("message:comment:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @Schema(description = "评论ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        commentService.deleteById(id);
        return R.ok();
    }

}
